string file = getenv("TMP1");
void eff(){
  TFile *f = new TFile(file.c_str(), "UPDATE");
  TTree *etf = (TTree *)f->Get("etf");

  const int n = etf->GetEntriesFast();
  const double d = n*0.01;
  const bool gl = etf->GetBranch("globalTracker");
  cout << "\n--------------- \033[32;1mTotal Tracking Efficiency\033[0m ------------------\n";
  cout << "preTaTrackerX: " << etf->GetEntries("Length$(preTaTrackerX)")/d << "%" << endl;
  cout << "postTaTrackerX: " << etf->GetEntries("Length$(postTaTrackerX)")/d << "%" << endl;
  cout << "postMagTrackerX: " << etf->GetEntries("Length$(postMagTrackerX)")/d << "%" << endl;
  if(gl) cout << "globalTracker: " << etf->GetEntries("Length$(globalTracker)")/d << "%" << endl;
  cout << "preTaTrackerY: " << etf->GetEntries("Length$(preTaTrackerY)")/d << "%" << endl;
  cout << "postTaTrackerY: " << etf->GetEntries("Length$(postTaTrackerY)")/d << "%" << endl;
  
  static const char *taTrkEff[4] = {"preTaTrkX", "preTaTrkY", "postTaTrkX", "postTaTrkY"};
  char cmd[128]{}, xy[2] = {'X', 'Y'}, ud[2] = {'U', 'D'};
  cout << "\n------------ \033[32;1mHard Efficiency: hit/nEvent\033[0m ------------\n";
  cout << "\t\033[1mX0\tX1\tY0\tY1\033[0m" << endl;
  for(int i = 0; i < 4; i++){
    // cout << endl << taTrkEff[i];
    cout << "dcTa" << ud[(i>>1)&1] << (i&1) << '\t';
    for(int j = 0; j < 4; j++){
      sprintf(cmd, "Length$(dcTa%c%d%c%d)>0", ud[(i>>1)&1], i&1, xy[(j>>1)&1], j&1);
      cout << etf->GetEntries(cmd) / d << '\t';
    } // end for over dc-layers around ta
    cout << endl;
  } // end for over i
  // cout << endl << "postMagTrkerX" << endl;
  cout << endl << "\033[1mdc0X0\tdc0X1\tdc1X0\tdc1X1\tdc2X0\tdc2X1\033[0m" << endl;
  for(int i = 0; i < 6; i++){
    sprintf(cmd, "Length$(dc%dX%d)>0", i/2, i&1);
    cout << etf->GetEntries(cmd) / d << '\t';
  } // end for over i
  cout << endl << endl;

  
  // soft efficiency: of all the tracks, the probability that a DC is not fired
  cout << "\n--------------- \033[32;1mSoft Efficiency: hitInTrk/nTrk\033[0m ------------------\n";
  // [0-1]: [X-Y]
  TClonesArray *cTaTrker[4] = { // [0-1-2-3]: [pre-X-Y--post-X-Y]
    new TClonesArray("ETFTrackTa"), new TClonesArray("ETFTrackTa"),
    new TClonesArray("ETFTrackTa"), new TClonesArray("ETFTrackTa")
  };
  TClonesArray *cGlobalTrker = nullptr;
  if(gl) cGlobalTrker = new TClonesArray("ETFTrackTa4");
  TClonesArray *cPostMagTrkerX = new TClonesArray("ETFTrack");
  etf->SetBranchAddress("preTaTrackerX", &cTaTrker[0]);
  etf->SetBranchAddress("preTaTrackerY", &cTaTrker[1]);
  etf->SetBranchAddress("postTaTrackerX", &cTaTrker[2]);
  etf->SetBranchAddress("postTaTrackerY", &cTaTrker[3]);
  if(gl) etf->SetBranchAddress("globalTracker", &cGlobalTrker);
  etf->SetBranchAddress("postMagTrackerX", &cPostMagTrkerX);

  // define the counter //
  int nTaTrker[4]{}, nGlobalTrker = 0, nPostMagTrkerX = 0; // # of trks
  int hTaTrker[4][4]{}, hGlobalTrker[8]{}, hPostMagTrkerX[6]{}; // # of hits

  for(int i = 0; i < n; i++){
    etf->GetEntry(i);
    // ta trackers //
    for(int j = 0; j < 4; j++){
      for(auto b : *cTaTrker[j]){
        ETFTrackTa *t = dynamic_cast<ETFTrackTa *>(b);
        for(auto k : t->GetLayerId()) hTaTrker[j][k]++;
        nTaTrker[j]++;
      } // end for over trks
    } // end for over trk-types
    if(gl) for(auto b : *cGlobalTrker){
      ETFTrackTa4 *t = dynamic_cast<ETFTrackTa4 *>(b);
      ETFTrackTa *tt[2] = {t->GetPreTaTrack(), t->GetPostTaTrack()};
      for(auto tr : tt) for(auto j : tr->GetLayerId()) hGlobalTrker[j]++;
      nGlobalTrker++;
    } // end for over global trks
    for(auto b : *cPostMagTrkerX){
      ETFTrack *t = dynamic_cast<ETFTrack *>(b);
      for(auto j : t->GetLayerId()) hPostMagTrkerX[j]++;
      nPostMagTrkerX++;
    } // end for over post-mag trks
    cout << "Going Through Event " << i  << ", ";
    cout << setw(5) << double(i)/n*100 << "% completed." << '\r' << flush;
  } // end for over entries

  cout << endl << "\033[1mdc0X0\tdc0X1\tdc1X0\tdc1X1\033[0m";
  for(int i = 0; i < 4; i++){
    cout << endl << taTrkEff[i] << endl;
    for(int j = 0; j < 4; j++) cout << hTaTrker[i][j] * 100. / nTaTrker[i] << '\t';
  } // end for over i
  if(gl){
    cout << endl << "globalTrker" << endl;
    cout << "\033[1mU0X0\tU0X1\tU1X0\tU1X1\tD0X0\tD0X1\tD1X0\tD1X1\033[0m" << endl;
    for(int i = 0; i < 8; i++) cout << hGlobalTrker[i] * 100. / nGlobalTrker << '\t';
  } // end if
  cout << endl << "postMagTrkerX" << endl;
  cout << "\033[1mdc0X0\tdc0X1\tdc1X0\tdc1X1\tdc2X0\tdc2X1\033[0m" << endl;
  for(int i = 0; i < 6; i++) cout << hPostMagTrkerX[i] * 100. / nPostMagTrkerX << '\t';
  cout << endl << endl;
} // end function eff
